package com.hzjq.encrypt.ui

import android.content.Context
import android.content.Intent
import com.hzjq.encrypt.adapter.ZbqyAdapter
import com.hzjq.encrypt.bean.EnBean
import com.hzjq.encrypt.bean.Zbqy
import com.hzjq.encrypt.bean.Zbqys
import com.hzjq.encrypt.pop.ZbqyEditorPop

class ZbqyActivity : AbsListActivity<Zbqy>() {

    companion object {
        fun start(context: Context, data: EnBean?) {
            val intent = Intent(context, ZbqyActivity::class.java)
            if (data == null) {
                intent.putExtra("data", EnBean())
            } else {
                intent.putExtra("data", data)
            }
            context.startActivity(intent)
        }
    }


    private val mZbqyAdapter = ZbqyAdapter(arrayListOf())

    override fun onDataCreated() {
        mAdapter = this.mZbqyAdapter

        if (data.zbqys == null) {
            data.zbqys = Zbqys()
        }
        if (data.zbqys.zbqy.isNullOrEmpty()) {
            data.zbqys.zbqy = arrayListOf()
        }

        mZbqyAdapter.setNewInstance(data.zbqys.zbqy)

        setShowTitle("准爆区域(${data.zbqys.zbqy.size})个")
    }

    override fun onRemoveItemClick(position: Int) {
        data.zbqys.zbqy.removeAt(position)
        setShowTitle("准爆区域(${data.zbqys.zbqy.size})个")
        notifyDataChanged()
    }

    override fun onItemEditorClick(position: Int) {
        ZbqyEditorPop.show(this,object : ZbqyEditorPop.OnZbqyEditorCallback{
            override fun onZbqyEditor(zbqy: Zbqy) {
                data.zbqys.zbqy[zbqy.position] = zbqy
                notifyDataChanged()
            }
        },mZbqyAdapter.getItem(position))
    }

    override fun onAddNewClick() {
        val lge = Zbqy()
        lge.position = -1
        ZbqyEditorPop.show(this,object : ZbqyEditorPop.OnZbqyEditorCallback{
            override fun onZbqyEditor(zbqy: Zbqy) {
                if (data.zbqys == null) {
                    data.zbqys = Zbqys()
                }
                if (data.zbqys.zbqy.isNullOrEmpty()) {
                    data.zbqys.zbqy = arrayListOf()
                }
                data.zbqys.zbqy.add(0, zbqy)
                mZbqyAdapter.setNewInstance(data.zbqys.zbqy)
                setShowTitle("准爆区域(${data.zbqys.zbqy.size})个")
                notifyDataChanged()
            }
        },lge)
    }
}